package com.zhanghp.class07;

/**
 * @author zhanghp
 * @since 2024/2/3 17:56
 */
public class Code03_SplitSort {

    public void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }


    public void splitSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int minIndex = -1;
        int temp = 0;
        int right = arr.length - 1;
        while (temp != arr.length) {
            if (arr[temp] <= arr[right]) {
                swap(arr, ++minIndex, temp++);
            } else {
                temp++;
            }
        }
    }

    public void splitSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int lIndex = -1;
        int rIndex = arr.length - 1;
        int pointer = 0;
        int fixVal = arr[arr.length - 1];

        while (pointer != arr.length - 1) {
            int curVal = arr[pointer];
            if (curVal < fixVal) {
                swap(arr, ++lIndex, pointer++);
            } else if (curVal == fixVal) {
                pointer++;
            } else {
                swap(arr, rIndex--, pointer++);
            }
        }
    }

    public void zuo(int[] arr){
        int N = arr.length;
        int lessR = -1;
        int moreL = N - 1;
        int index = 0;
        // arr[N-1]
        while (index < moreL) {
            if (arr[index] < arr[N - 1]) {
                swap(arr, ++lessR, index++);
            } else if (arr[index] > arr[N - 1]) {
                swap(arr, --moreL, index);
            } else {
                index++;
            }
        }
        swap(arr, moreL, N - 1);
    }
}
